﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net.FtpClient;
using System.IO;
using System.Configuration;

namespace CloudStorageLight.Core.Tests
{
    [TestClass]
    public class FtpBlobAdapterTest
    {
        string FtpUrl = ConfigurationManager.AppSettings["FtpUrl"];
        string FtpUser = ConfigurationManager.AppSettings["FtpUser"];
        string FtpPassword = ConfigurationManager.AppSettings["FtpPassword"];

        [TestMethod]
        public void TestMethod1()
        {
            using (var ftp = new FtpClient())
            {
                ftp.Host = FtpUrl;
                ftp.Credentials = new System.Net.NetworkCredential(FtpUser, FtpPassword);
                foreach (FtpListItem item in ftp.GetListing(ftp.GetWorkingDirectory(),
                     FtpListOption.Modify | FtpListOption.Size))
                {

                    switch (item.Type)
                    {
                        case FtpFileSystemObjectType.Directory:
                            break;
                        case FtpFileSystemObjectType.File:
                            break;
                        case FtpFileSystemObjectType.Link:
                            // derefernece symbolic links
                            if (item.LinkTarget != null)
                            {
                                // see the DereferenceLink() example
                                // for more details about resolving links.
                                item.LinkObject = ftp.DereferenceLink(item);

                                if (item.LinkObject != null)
                                {
                                    // switch (item.LinkObject.Type)...
                                }
                            }
                            break;
                    }
                }
                
            }
        }

        [TestMethod]
        public void ConvertFtpPathToUrlPathTest()
        {
            using(var adp = new FtpBlobAdapter("/Ftp/", FtpUrl, FtpUser, FtpPassword, "/"))
            {
                var value = adp.ConvertFtpPathToUrlPath("/Sub1/A.txt");

                Assert.AreEqual("/Ftp/Sub1/A.txt", value);

            }
        }

        [TestMethod]
        public void ConvertUrlPathToFtpPathTest()
        {
            using(var adp = new FtpBlobAdapter("/Ftp/", FtpUrl, FtpUser, FtpPassword, "/"))
            {
                var value = adp.ConvertUrlPathToFtpPath("/Ftp/Sub1/A.txt");

                Assert.AreEqual("/Sub1/A.txt", value);

            }
        }

        [TestMethod]
        public void UploadAsyncAndDeleteAsyncCopyAsyncTest()
        {
            using(var adp = new FtpBlobAdapter("/Ftp/", FtpUrl, FtpUser, FtpPassword, "/"))
            {
                var temp = Path.GetTempFileName();
                try
                {
                    var itemA = new BlobFile(null, "/Ftp/A.txt");
                    var itemB = new BlobFile(null, "/Ftp/Sub1/SUb2/B.txt");
                    var itemBDire = new BlobDirectory(null, "/Ftp/Sub1/");
                    var itemB2Dire = new BlobDirectory(null, "/Ftp/Sub1/SUb2/");
                    adp.DeleteAsync(itemA).Wait();
                    adp.DeleteAsync(itemB).Wait();
                    adp.DeleteAsync(itemB2Dire).Wait();
                    adp.DeleteAsync(itemBDire).Wait();

                    var value = adp.ExistsAsync("/Ftp/A.txt").Result;
                    Assert.AreEqual(false, value);

                    adp.UploadAsync(itemA, temp).Wait();
                    value = adp.ExistsAsync("/Ftp/A.txt").Result;
                    Assert.AreEqual(true, value);

                    adp.CopyAsync("/Ftp/Sub1/SUb2/B.txt", itemA).Wait();
                    value = adp.ExistsAsync("/Ftp/Sub1/SUb2/B.txt").Result;
                    Assert.AreEqual(true, value);

                }
                finally
                {
                    File.Delete(temp);
                }

            }
        }

        [TestMethod]
        public void CreateDirectoryAsyncTest()
        {
            using(var adp = new FtpBlobAdapter("/Ftp/", FtpUrl, FtpUser, FtpPassword, "/"))
            {
                var temp = Path.GetTempFileName();
                try
                {
                    var itemDire = new BlobDirectory(null, "/Ftp/SubTemp/");
                    adp.DeleteAsync(itemDire).Wait();

                    var value = adp.ExistsAsync("/Ftp/SubTemp/").Result;
                    Assert.AreEqual(false, value);

                    adp.CreateDirectoryAsync(itemDire).Wait();
                    value = adp.ExistsAsync("/Ftp/SubTemp/").Result;
                    Assert.AreEqual(true, value);


                }
                finally
                {
                    File.Delete(temp);
                }

            }
        }

        [TestMethod]
        public void GetFilesAsyncTest()
        {
            using(var adp = new FtpBlobAdapter("/Ftp/", FtpUrl, FtpUser, FtpPassword, "/"))
            {
                var list = adp.GetFilesAsync("/Ftp/", 0, false).Result;
                Assert.IsTrue(list.Count > 0);

                var itemBDire = new BlobDirectory(null, "/Ftp/Sub1/Sub2/");
                adp.CreateDirectoryAsync(itemBDire).Wait();
                var list2 = adp.GetFilesAsync("/Ftp/Sub1/", 0, true).Result;
                Assert.IsTrue(list2.Count > 0);
            }

        }
    }
}
